<script lang="ts" setup>
import { batchRemoveSeriesData, removeSeriesData } from "@@/apis/video/series"
import { onMounted, ref } from "vue"
import { useForm } from "./useForm"
import { useTable } from "./useTable"

const {
  tableData,
  searchParams,
  currentPage,
  pageSize,
  total,
  fetchData,
  handlePageChange
} = useTable()

const {
  formData,
  formRules,
  formDataRef,
  addDialogVisible,
  isEdit,
  showDialog,
  submitForm,
  closeDialog
} = useForm(fetchData)

const selectable = (row: any) => ![1, 2].includes(row.id)

const roleOptions = [
  { label: "管理员", value: "admin" },
  { label: "用户", value: "user" }
]

function handleEdit(row: any) {
  formData.value = { ...row }
  isEdit.value = true
  showDialog()
}
const multipleSelection = ref([])
function handleSelectionChange(val: []) {
  multipleSelection.value = val
}

function handleRemove(row: any) {
  ElMessageBox.confirm(
    "确认删除吗?",
    "提示！",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(async () => {
      await removeSeriesData(row.id)
      fetchData()
      ElMessage.success("删除成功！")
    })
}

function batchDelete() {
  console.log(multipleSelection)
  if (multipleSelection.value.length === 0) return ElMessage.warning("请选择要删除的项")

  ElMessageBox.confirm(
    "确认删除吗?",
    "提示！",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(async () => {
      const ids = multipleSelection.value.map((item: any) => item.id)
      await batchRemoveSeriesData({ ids })
      fetchData()
      ElMessage.success("删除成功！")
    })
}

// 初始化加载数据
onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="view-container">
    <!-- 操作栏 -->
    <div class="view-header">
      <div>
        <el-input v-model="searchParams.username" placeholder="输入用户名搜索" style="width: 300px; margin-right: 10px;" />
        <el-button type="primary" @click="fetchData">
          搜索
        </el-button>
      </div>
      <div>
        <el-button type="primary" @click="showDialog">
          添加用户
        </el-button>
        <el-button type="danger" @click="batchDelete">
          批量删除
        </el-button>
      </div>
    </div>

    <!-- 列表 -->
    <div class="view-main">
      <el-table :data="tableData" @selection-change="handleSelectionChange" border style="height:100%;width: 100%">
        <el-table-column type="selection" :selectable="selectable" width="55" />
        <el-table-column prop="username" label="用户名" width="260" />
        <el-table-column prop="role" label="权限" width="260" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createTime" label="创建日期" width="160" />
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button link type="danger" size="small" @click="handleRemove(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="view-footer">
      <el-pagination
        background layout="prev, pager, next" :total="total" :page-size="pageSize"
        v-model:current-page="currentPage" @current-change="handlePageChange"
      />
    </div>

    <!-- 添加对话框 -->
    <el-dialog
      v-model="addDialogVisible" :title="isEdit ? '编辑用户' : '添加用户'" :close-on-click-modal="false"
      @close="closeDialog"
    >
      <el-form :model="formData" label-width="100px" :rules="formRules" ref="formDataRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="权限" prop="role">
          <el-select v-model="formData.role" placeholder="请选择权限">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" :rows="4" type="textarea" />
        </el-form-item>
        <el-form-item label-width="0">
          <div style="width: 100%;display: flex;justify-content: center;gap: 20px;">
            <el-button style="width: 120px;" @click="closeDialog">
              取 消
            </el-button>
            <el-button style="width: 120px;" type="primary" @click="submitForm(formDataRef)">
              提 交
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.top-badge {
  .top-badge-bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 16px 16px 0;
    /* 右上方向的三角形 */
    border-color: transparent red transparent transparent;
    /* 红色三角形 */
    transform: rotate(-90deg);
    /* 斜着的效果 */
    margin: 2px 0 0 2px;
    /* 微调位置 */
  }
}
</style>
